import { resUrlBase } from '../serveices/util.js'

export function checkUpdater(currentId, updaterPage) {
    uni.request({
        url: resUrlBase + 'upgrade.ashx?action=lastVersion',
        method: 'GET',
        data: {
            search: 'latestId'
        },
        success: res => {
            if (res.statusCode === 200) {
                const response = res.data
                const latestId = response.version
                if (currentId != latestId) {
                    console.log('检测到更新')
                    uni.showModal({
						showCancel:false,
                        title: '发现新版本',
                        content: '有新版本可用 (版本id:' + latestId + ')，请问您是否更新？',
                        success: (res) => {
                            if (res.confirm) {
                                uni.navigateTo({
                                    url: updaterPage
                                })										
                            } else if (res.cancel) {
                                console.log('取消')
                            }
                        }
                    })
                } else {
                    console.log('现在是最新版本')
                }      
            }
        },
		fail(err) {
			console.error(err)
		}
    })
}